<?php
namespace app\admin\controller;
use app\admin\model\CommunityModel;
use think\Controller;
use \think\Request;
use think\Session;
use app\admin\controller\Base;
class  Community extends Base
{
    /**
     * 社区列表
     */
    public function index()
    {
        $this->isLogin();
        $model = new CommunityModel();
        $res = $model->selectAll();
        $this->assign('res', $res);
        return $this->fetch();
    }

    /*
     * 社区创建(显示)
     */
    public function createCommunityView()
    {
        return $this->fetch('createcommunity');
    }

    /*
     *社区创建（操作）
     */
    public function createCommunityList()
    {
        if (request()->isPost()) {
            $community_url = input('community_url');
            $data = [
                'community_name' => input('community_name'),
                'community_url' => $community_url,
                'is_del' => 0,
                'create_time' => time()
            ];

            $model = new CommunityModel();
            $row = $model->insertRow($data);
            return $row;

        }

    }

    /*
*上传图片
*/
    public function addKnowsList(){
        if(Request::instance()->isPost()){
            $file = request()->file('img');
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload'.DS.'community');
            if($info){
                echo $path ='upload'.'/'.'community'.'/'.date('Ymd').'/'.$info->getFilename();
            }else{
                echo $file->getError();
            }
        }
    }

    /*
     *修改社区(显示 )
     */
    public function updateCommunityView()
    {
        $id = input('id');
        $model = new CommunityModel();
        $res = $model->selectFind($id);

        $this->assign('res', $res);
        return $this->fetch('updateCommunity');
    }

    /*
     *修改社区(操作)
     */
    public function updateCommunityList()
    {
        $community_url = input('community_url');
        $data = [
            'id' => input('id'),
            'community_name' => input('community_name'),
            'community_url' => $community_url,
            'is_del' => 0,
            'create_time' => time()
        ];

        $model = new CommunityModel();
        $row = $model->updateRow($data);
        return $row;

    }



    /*
     *删除社区(逻辑删除)
     */
    public function delCommunity()
    {
        $id = input('id');
        $is_del = 1;
        $data = [
            'id' => $id,
            'is_del' => $is_del
        ];
        $model = new CommunityModel();
        $res = $model->updateAll($data);
        return $res;
    }



}